__author__ = 'AndreyKa'

import time
import os
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import *
from selenium.webdriver.common.by import By
from php4dvd.pages.movie_page import MoviePage
from php4dvd.pages.loginpage import LoginPage
from php4dvd.pages.internalpage import InternalPage
from php4dvd.pages.userprofilepage import UserProfilePage
from php4dvd.pages.addmoviepage import AddMoviePage
from php4dvd.model.user import User
import random as randomer


class Application(object):
    """
    This class describes the logic of application.
    """

    def __init__(self, driver, base_url):
        driver.get(base_url)
        self.wait = WebDriverWait(driver, 20)
        self.lp = LoginPage(driver, base_url)
        self.ip = InternalPage(driver, base_url)
        self.upp = UserProfilePage(driver, base_url)
        self.m = MoviePage(driver, base_url)
        self.am = AddMoviePage(driver, base_url)

#login

    def login(self, user):
        time.sleep(1)
        self.lp.username_field.clear()
        self.lp.username_field.send_keys(user.username)
        self.lp.password_field.clear()
        self.lp.password_field.send_keys(user.password)
        self.lp.submit_button.click()

    def logout(self):
        self.ip.logout_button.click()
        self.wait.until(alert_is_present()).accept()

    def is_logged_in(self):
        return self.ip.is_this_page

    def is_not_logged_in(self):
        return self.lp.is_this_page

    def is_logged_in_as(self, user):
        return self.is_logged_in() and self.get_logged_user().username == user.username

    def get_logged_user(self):
        self.ip.user_profile_link.click()
        self.upp.is_this_page
        return User(username=self.upp.username_field.get_attribute("value"),
                    email=self.upp.email_field.get_attribute("value"))

    def ensure_login_as(self, user):
        element = self.wait.until(presence_of_element_located((By.CSS_SELECTOR, "nav, #loginform")))
        if element.tag_name == 'nav':
            if self.is_logged_in_as(user):
                return
            else:
                self.logout()
        self.login(user)

    def ensure_logout(self):
        element = self.wait.until(presence_of_element_located((By.CSS_SELECTOR, "nav, #loginform")))
        if element.tag_name == 'nav':
            self.logout()

#search film

    def v_search(self, search):
        self.ip.search_field.clear()
        self.ip.search_field.send_keys(search.search_inf, Keys.ENTER)
        self.ip.wait.until(presence_of_element_located((By.XPATH, ".//*[@id='results']")))

    def v_search_result(self, search):
        if self.am.wait.until(presence_of_element_located((By.XPATH, "//*[contains(text(),'"+search.search_inf+"')]"))) != "":
            return True
        else:
            return False

    def inv_search_result(self):
        if self.am.wait.until(presence_of_element_located((By.XPATH, "//*[contains(text(),'where found')]"))) != "":
            return True
        else:
            return False

#add movie

    def add_movie(self, add_movie, avatar_name):
        self.ip.add_movie.click()
        self.am.title_field.send_keys(add_movie.title)
        self.am.year_field.send_keys(add_movie.year)
        self.ip.wait.until(presence_of_element_located((By.XPATH, ".//*[@type='file']")))
        my_path, my_file = os.path.split(os.path.realpath(__file__))
        film_cover = '%s\\%s' % (my_path, avatar_name.name)
        self.am.file_button.send_keys(os.path.abspath(film_cover))
        time.sleep(1)
        self.am.submit_button.click()

    def ensure_add_movie(self):
        if self.am.wait.until(presence_of_element_located((By.XPATH, "//div[@class='photo']"))) != "":
            return True
        else:
            return False

    def ensure_fail_add_movie(self):
        if self.am.wait.until(presence_of_element_located((By.XPATH, ".//label[@class='error']"))) != "":
            return True
        else:
            return False

#delete movie

    def delete_movie(self):
            self.ip.wait.until(presence_of_element_located((By.XPATH, ".//*[@class='movie_box']")))
            rnd = randomer.choice(self.ip.driver.find_elements(By.XPATH, ".//div[@class='movie_box']")).get_attribute("id")
            my_path, my_file = os.path.split(os.path.realpath(__file__))
            film_path = '%s\\%s' % (my_path, 'id_container')
            f = open(film_path, 'w')
            f.write(rnd)
            f.close()
            self.ip.driver.find_element(By.XPATH, ".//div[@id='%s']" % rnd).click()
            self.m.remove_link.click()
            self.wait.until(alert_is_present()).accept()
            return None

    def ensure_deleting(self):
            my_path, my_file = os.path.split(os.path.realpath(__file__))
            film_path = '%s\\%s' % (my_path, 'id_container')
            f = open(film_path, 'r')
            rnd = f.read()
            f.close()
            if presence_of_element_located((By.XPATH, ".//div[@id='%s']" % rnd)) != "":
                return True
            else:
                return False






